package viz_ui;

import processing.core.PApplet;
import utilities.EventListener;
import utilities.EventOwner;
import view.DisplayList;
import viz_main.ScatterPlotAxis;
import controllers.ControlManager;
import buttons.VerticalDoubleSlider;
import buttons.control.C_ToggleButton;
import buttons.control.C_VerticalDoubleSlider;
import buttons.view.V_CheckBox;
import buttons.view.V_ToggleButton;
import buttons.view.V_VerticalDoubleSlider;

/* * *
 * An alternate view of an VerticalDoubleSlider that behaves in a very similar way. It is used as 
 * an interactive component for displaying a parallel axis. Additionally this version supports:
 *  - toggling the filter on/off  
 *  - having a handle for dragging the axis around horizontally (discrete or fluid)
 * * */
public class ParallelAxis extends VerticalDoubleSlider implements EventListener{

	private V_CheckBox vToggle;
	private C_ToggleButton cToggle;
	private final float boxSize = 10;
	private ParallelView directParent;
	private ScatterPlotAxis axis;
	
	public ParallelAxis(DisplayList v, ControlManager c, String n, float xWidth, float yHeight, PApplet p, ParallelView dParent) {
		super(v, c, n, xWidth, yHeight);
		vToggle.useFont(p); // set toggle button to use its font so that it can display text
		((ParallelAxisSlider)vButton).useFont(p);
		cToggle.addSubscriber(this);
		cButton.setEnabled(false);
		directParent = dParent;
	}
	@Override
	protected void generateButtons(DisplayList v, ControlManager c, String n, float xWidth, float yHeight)
	{
		vButton = new ParallelAxisSlider(n, xWidth, yHeight);
	    cButton = new C_VerticalDoubleSlider((V_VerticalDoubleSlider)vButton);
	    vToggle = new V_CheckBox("filter:", boxSize, boxSize); // create toggle button view
	    cToggle = new C_ToggleButton(vToggle); // create toggle button controller
	    addToManagerLists(v, c);
	}
	
	@Override
	public void display(PApplet p) {
		// because this component has more display elements than other buttons, we need to override
		// the normal display method.
		((ParallelAxisSlider)vButton).display(p);
		vToggle.display(p);
	}
	

	public void setAxis(ScatterPlotAxis axis)
	{
		this.setRange(axis.getMax(), axis.getMin());
		this.axis = axis;
	}
	
	public ScatterPlotAxis getAxis()
	{
		return axis;
	}
	
	public void setLocation(float x, float y)
	{
		vButton.setLocation(x, y);
		vToggle.setLocation(x+5, y + vButton.getYSize() + 20);
	}
	
	public void setXLocation(float x)
	{
		vButton.setXLoc(x);
		vToggle.setXLoc(x+5);
	}
	
	public void setRange(float max, float min)
	{
		((ParallelAxisSlider)vButton).setRange(max, min);
	}
	
	@Override
	public void handleEvent(String trigger, EventOwner obj) {
		// Used to allow toggling the filter on/off
		if(trigger.equals("toggleOn")) {
			((ParallelAxisSlider)vButton).setShowSliders(true);
			cButton.setEnabled(true);
			directParent.handleEvent(trigger, obj);
			// TODO: notify the parent that filtering has been enabled
		}
		else if (trigger.equals("toggleOff"))
		{
			((ParallelAxisSlider)vButton).setShowSliders(false);
			cButton.setEnabled(false);
			directParent.handleEvent(trigger, obj);
			// TODO: notify the parent that filtering has been disabled
		}
	}
	
	@Override
	protected void addToManagerLists(DisplayList v, ControlManager c)
	{	
		// add the proper component to their proper list if that list is provided as non-null
		// we override the default Button version because this component has extra elements in it
		if(v != null) { // button is managed by displayList
			vButton.addSelfToList(v);
			vToggle.addSelfToList(v);
		}
		if(c != null){ // button is managed by controlManager
			cButton.addSelfToList(c);
			cToggle.addSelfToList(c);
		}
	}
	public boolean isFilterEnabled() {
		// TODO Auto-generated method stub
		return vToggle.getToggle();
	}
}
